package com.cloudpivot.project.crmprojectnew.controller;

import com.cloudpivot.project.crmprojectnew.pojo.ResultJson;
import com.cloudpivot.project.crmprojectnew.pojo.accountequipment.AccountEquipment;
import com.cloudpivot.project.crmprojectnew.pojo.accountequipment.AccountEquipmentList;
import com.cloudpivot.project.crmprojectnew.service.impl.AccountEquipmentServiceImpl;
import com.cloudpivot.project.crmprojectnew.util.CrmNormalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;


/**
 * 客户设备
 */
@Controller
@Slf4j
@RequestMapping("/accountEquipment")
public class AccountEquipmentController {

    @Autowired
    private AccountEquipmentServiceImpl accountEquipmentService;

    /**
     * 获取客户设备记录(单条)
     */
    @GetMapping("getInfo")
    @ResponseBody
    public AccountEquipment getInfo(@RequestParam(value = "guid") String guid){
        AccountEquipment accountEquipment=accountEquipmentService.getAccountEquipment(guid);
        /*
        转换时间格式
         */
        changeGMTDateTime(accountEquipment);
       return accountEquipment;
    }

/*    @GetMapping("getInfo")
    @ResponseBody
    public Map<String,Object> getInfoTest(String guid){
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("id","926d83fd-e878-ea11-8abe-0050569a16cf");
        objectMap.put("displayCode","new_name");
        objectMap.put("new_name", "电梯模块");
        objectMap.put("schemaCode", "SBMC");
        objectMap.put("propertyType", 0);
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("new_equipment", 1);
        returnMap.put("new_name", "test");
        returnMap.put("new_trademark", "123");
        returnMap.put("new_product_value", objectMap);
        return returnMap;
    }*/

    /**
     * 获取客户设备列表
     * @param userid
     * @return
     */
    @GetMapping("getInfos")
    @ResponseBody
    public AccountEquipmentList getInfos(@RequestParam(value = "userid") String userid,@RequestParam(value="pageIndex") int pageIndex,@RequestParam(value="pageSize") int pageSize){
        return accountEquipmentService.getAccountEquipments(userid,pageIndex,pageSize);
    }

    private void changeGMTDateTime(AccountEquipment a){
        a.setNew_installation_date(CrmNormalUtil.convertGMTDateTimeToNormal(a.getNew_installation_date()));
        a.setNew_reception_time(CrmNormalUtil.convertGMTDateTimeToNormal(a.getNew_reception_time()));
        a.setNew_manufacturedate(CrmNormalUtil.convertGMTDateTimeToNormal(a.getNew_manufacturedate()));
    }

    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultJson exceptionHandler(MethodArgumentNotValidException e) {
        ResultJson resultJson = new ResultJson(-1, "error");
        resultJson.setMsg(e.getBindingResult().getFieldError().getDefaultMessage());
        return resultJson;
    }
}
